﻿using System;
using System.Xml;
using System.Xml.Schema;
using System.Collections.Generic;
using System.IO;

namespace LAPI.Core
{
    public class XmlValidador
    {
        public IList<string> errores = new List<string>();

        public XmlValidador(string xsdFile, string xmlString)
        {
            TextReader xmlReader = new StreamReader(xmlString);
            XmlReaderSettings booksSettings = new XmlReaderSettings();
            XmlSchemaSet set = new XmlSchemaSet();
            set.Add(null, xsdFile);
            

            booksSettings.ValidationType = ValidationType.Schema;
            booksSettings.Schemas = set;
            booksSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
            booksSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
            booksSettings.ValidationEventHandler += new ValidationEventHandler(BooksSettingsValidationEventHandler);

            XmlReader books = XmlReader.Create(xmlReader, booksSettings);

            while (books.Read()) { }

        }

        public void BooksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
        {

            if (e.Severity == XmlSeverityType.Warning)
            {
                //Console.Write("WARNING: ");
                //Console.WriteLine(e.Message);

                //res = "WARNING";
                errores.Add(e.Message);
            }
            else if (e.Severity == XmlSeverityType.Error)
            {
                //Console.Write("ERROR: ");
                //Console.WriteLine(e.Message);
                //res = "ERROR";
                errores.Add(e.Message);
            }
        }

        public IList<string> GetErrores()
        { 
            return errores;
        }
    }
}